package com.lg;

import java.io.IOException;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.config.Ini;
import org.apache.shiro.io.ResourceUtils;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.realm.text.PropertiesRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

public class InitRealmTest2 {

	@Test
	public void test01() throws IOException {
		IniRealm iniRealm = new IniRealm("classpath:shiro.ini");
		DefaultSecurityManager securityManager = new DefaultSecurityManager();
		securityManager.setRealm(iniRealm);
		SecurityUtils.setSecurityManager(securityManager);
		Subject subject = SecurityUtils.getSubject();
		AuthenticationToken token = new UsernamePasswordToken("wang", "123");
		try {
			subject.login(token);
			boolean authenticated = subject.isAuthenticated();
			System.out.println(authenticated);
//			subject.checkRole("administrator");
//			subject.checkPermissions("start");
			subject.logout();
			System.out.println(subject.isAuthenticated());
		} catch (UnknownAccountException e) {
			// TODO Auto-generated catch block
			System.out.println("账户不存在：" + e);
		} catch (IncorrectCredentialsException e) {
			// TODO Auto-generated catch block
			System.out.println("密码不正确：" + e);
		} catch (LockedAccountException e) {
			// TODO Auto-generated catch block
			System.out.println("账户已锁定：" + e);
		} catch (ExpiredCredentialsException e) {
			// TODO Auto-generated catch block
			System.out.println("密码已过期：" + e);
		} catch (AuthenticationException e) {
			// TODO Auto-generated catch block
			System.out.println("认证异常：" + e);
		} catch (AuthorizationException e) {
			// TODO Auto-generated catch block
			System.out.println("异常：" + e);
		}
	}
}
